<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Signin extends Model
{
    protected $table = "c_sign_in";

    public static function add($filter)
    {
        try {
            DB::beginTransaction(); // 开始事务
            $date = date("Y-m-d", time());
            $info = self::where([['work_id', '=', $filter['work_id']], ['date_at', '=', $date]])->first();
            if ($info) {
                return "今日您已经签到!";
            }
            $sign = new self();
            $sign->date_at = $date;
            $sign->work_id = $filter['work_id'];
            $res = $sign->save();
            if ($res) {
                //增加成长值
                TaskComplete::add($filter, 1);
                GrowthNumberLog::add($filter,1);
                DB::commit(); // 提交事务
                return true;
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }
}
